package com.sjz.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sjz.adapter.PersonAdapter;
import com.sjz.domain.Person;
import com.sjz.service.PersonService;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class DbActivity extends Activity {
	 private ListView listView;
	 
	  private PersonService personService;
	  
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        personService = new PersonService(this);
        listView = (ListView) this.findViewById(R.id.listView); //得到listview组件
        listView.setOnItemClickListener(new ItemClickListener());  //点击条目点击事件
        show3();
    }
    
    /***
     * 条目点击事件触发的类
     * @author Administrator
     *
     */
    private final class ItemClickListener implements OnItemClickListener{
    	// parent  当前条目被点击时的listview  当前点击的view对象    position 当前绑定的数组对应的索引值    id在集合中的排序 很少用到
 		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 			ListView lView = (ListView)parent;
 			/* 自定义适配器  调用show3得到的值  	*/
 			Person person = (Person) lView.getItemAtPosition(position);   //获取给定索引对应的数据
 			Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();
 			
 		   // 这是调用show2() 点击行得到的值   得到值的方法不一样 
 			/****
 			Cursor cursor = (Cursor) lView.getItemAtPosition(position);
 			int personid = cursor.getInt(cursor.getColumnIndex("_id"));
 			Toast.makeText(getApplicationContext(), personid+ "", 1).show();
 			***/
 		}
     }
    
    /****
     * 自定义适配器  PersonAdapter
     */
  	private void show3() {
  		List<Person> persons = personService.getScrollData(0, 20);
  		PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
  		listView.setAdapter(adapter);
  	}
    
    /****
     * 使用SimpleCursorAdapter展示数据
     * 需要注意的是 查询的值中必须要保包含_id 如果没有的话，就会报错，
     * 这里是把其中的一个值，设置成_id( personid as _id)  查询条件中来设置
     */
    private void show2() {
		Cursor cursor = personService.getCursorScrollData(0, 20);
		///Context context, int layout, Cursor c, String[] from, int[] to
		//第二个参数是设置条目的模板  
		SimpleCursorAdapter adapter =
				new SimpleCursorAdapter(this, R.layout.item, cursor,
				new String[]{"name", "phone", "amount"}, 
				new int[]{R.id.name, R.id.phone, R.id.amount});
		listView.setAdapter(adapter);
	}
    
    
    /****
     * 使用 SimpleAdapter来实现展示列表
     */
	private void show() {
		List<Person> persons = personService.getScrollData(0, 20);
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
		for(Person person : persons){
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("name", person.getName());
			item.put("phone", person.getPhone());
			item.put("amount", person.getAmount());
			item.put("id", person.getId());
			data.add(item);
		}
		//Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to
		//第三个参数是数据要绑定到哪个条目上   item.xml
		//第四个和第五个参数 把条目中对应的值 绑定到对应的组件上  第四个参数是 设置的map中的key
		//第五个参数的内容是item.xml中的组件的名称
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
				new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
		
		listView.setAdapter(adapter);   //给listview 设置自配器对象
		
	}
}